import {Download, Printer} from "eleicon";
import api from "@/api";
import utils from "utils";
import printJS from "print-js";
import {ref} from "vue";


const loading = ref(false)
const loadingOnline = ref(false)

type ExportFileProps = {
  id: number,
  funcname: string,
  filename: string
}

export default ({id, funcname, filename}: ExportFileProps, ext: any) => {

  const downClick = () => {
    loading.value = true;
    // @ts-ignore
    api[funcname](id).then((res: any) => {
      loading.value = false;
      if (!res.ResponseError) {
        utils.downloadFile(res.data, filename);
      }
    })
  }
  const DownloadOnline = () => {
    loadingOnline.value = true;
    // @ts-ignore
    api[funcname](id, {link: 1}).then((res: any) => {
      loadingOnline.value = false;
      if (!res.ResponseError) {
        console.log('open to ' + res.data.path);
        goPrint(res.data.path)
      }
    })
  }
  const goPrint = (path: string) => {
    printJS({
      printable: path, type: 'pdf', showModal: true, modalMessage: '正在打印中...',
      onPrintDialogClose: () => {console.log('打印完成')}
    })
  }

  return (
    <div class='i-center'>
      <el-button icon={Download} v-loading={loading.value} disabled={loading.value} onClick={downClick}>导出</el-button>
      <el-button icon={Printer} v-loading={loadingOnline.value} disabled={loadingOnline.value} onClick={DownloadOnline}>在线打印</el-button>
      {ext.slots.default && ext.slots.default()}
    </div>
  )
}